package com.lovo.bbs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.RegisterBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.LoginVo;
import com.lovo.bbs.vo.RegisterVo;

/**
 * 注册Servlet
 * 
 * @author tiancen2001
 * 
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 2718532453037623034L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@SuppressWarnings("unchecked")
	public synchronized void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");

		String qq = request.getParameter("qq");
		String msn = request.getParameter("msn");
		String blog = request.getParameter("blog");
		String site = request.getParameter("site");// 个人主页

		String birthday =request.getParameter("birthday");
		
		// 来自,籍贯
		String location =new String(request.getParameter("location").getBytes("ISO-8859-1"),"utf-8");
		
		RegisterVo registerVo = new RegisterVo();
		registerVo.setUsername(username);
		registerVo.setPassword(password);
		registerVo.setEmail(email);

		registerVo.setQq(qq);
		registerVo.setMsn(msn);
		registerVo.setBlog(blog);
		registerVo.setHomePage(site);
		registerVo.setBirthday(birthday);
		registerVo.setHome(location);

		HttpSession session = request.getSession();
		String msg = null;// 返回消息

		// 注册
		ForumStatBo forumStatBo = new ForumStatBo();
		RegisterBo registerBo   =  new RegisterBo();
		if (registerBo.addUser(registerVo) == 1) {
			msg = "1";// 注册成功
			LoginVo loginvo = new LoginVo();
			loginvo.setUserName(username);
			loginvo.setPassword(password);
			session.setAttribute("loginvo", loginvo);// 记录登录状态

			// 将用户加入在线列表
			ServletContext contex = request.getSession().getServletContext();
			HashMap<String, LoginVo> onlineUser = null;
			onlineUser = (HashMap<String, LoginVo>) contex
					.getAttribute("onlineUser");
			onlineUser.put(loginvo.getUserName(), loginvo);
			contex.setAttribute("onlineUser", onlineUser);
			
			//更新网站杂项信息,同时更新数据库和内存数据
			forumStatBo.addMember();//更新数据库
			ForumStatVo forumInfo = (ForumStatVo)contex.getAttribute("forumInfo");
			//更新内存网站杂项信息
			if(forumInfo==null){
				forumInfo = forumStatBo.getForumInfo();
			}
			//会员数加一
			forumInfo.setMemberNum(forumInfo.getMemberNum()+1);
			contex.setAttribute("forumInfo", forumInfo);
			request.setAttribute("forumInfo", forumInfo);

		} else if (registerBo.addUser(registerVo) == 0) {
			msg = "0";// 用户已存在
		} else if (registerBo.addUser(registerVo) == -1) {
			msg = "-1";// 出现异常
		}

		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(msg);
		out.flush();
		out.close();
	}
}
